home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / CodeWarrior Lite / Metrowerks C⁄C++ Lite / Libraries / Runtime / Runtime PPC / Sources / StdCStartup.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-16  |  2.7 KB  |  111 lines  |  [TEXT/MPCC]

  1.  
  2. /*
  3.  *    Startup.c        -    Default init/startup/termination routines for Metrowerks C++ (PowerPC)
  4.  *
  5.  *    Copyright © 1993 metrowerks inc. All Rights Reserved.
  6.  *
  7.  */
  8.  
  9. #include <MWException.h>
  10. #include <Quickdraw.h>
  11. #include <SegLoad.h>
  12. #include <setjmp.h>
  13. #include <stdlib.h>
  14.  
  15.  
  16.     /*    public data        */
  17.  
  18. QDGlobals qd;                        /*    define the Quickdraw globals here!    */
  19. jmp_buf __program_exit;                /*    exit() does a longjmp() to here        */
  20. void (*__atexit_hook)(void);        /*    atexit()  sets this up if it is ever called    */
  21. void (*___atexit_hook)(void);        /*    _atexit() sets this up if it is ever called    */
  22. int __aborting;                        /*    abort() sets this and longjmps to __program_exit    */
  23.  
  24.  
  25.      /*    external references    */
  26.  
  27. extern int main(long argc, char **argv, char **envp);
  28.  
  29.  
  30.     /*    prototypes    */
  31.  
  32. void __sinit(void);
  33. int __start(void);
  34.  
  35.  
  36. /*
  37.  *    __sinit        -    stub for static initialization of C++ objects
  38.  *
  39.  *    The linker replaces the contents of this module with something of the form:
  40.  *
  41.  *            mflr    r0
  42.  *            stw        r0,8(sp)
  43.  *            stwu    sp,-64(sp)
  44.  *            bl        __sinit_<file1>
  45.  *            bl        __sinit_<file2>
  46.  *            ...
  47.  *            bl        __sinit_<fileN>
  48.  *            addi    sp,sp,64
  49.  *            lwz        r0,8(sp)
  50.  *            mtlr    r0
  51.  *            blr
  52.  */
  53.  
  54. void __sinit(void)
  55. {
  56. }
  57.  
  58.  
  59. /*
  60.  *    __start    -    Default startup routine for Metrowerks C++ (PowerPC)
  61.  *
  62.  *    This routine should be specified as the PEF main routine in the container
  63.  *    for any application. The program startup/termination sequence is:
  64.  *
  65.  *    1.    Set up a jmp_buf for exit() to jump to
  66.  *    2.    Call all static initializers--these might call exit()
  67.  *    3.    Set up default values for 'argc' and 'argv' and call main()
  68.  *    4.    Execute any atexit() routines
  69.  *    5.    Destroy all static objects
  70.  *    6.    Execute any cleanup code, e.g. close open files, console window, etc.
  71.  *    7.    Terminate the Macintosh program
  72.  *
  73.  *    We postulate that there are 2 kinds of atexit() routines: those which must
  74.  *    execute before any  objects are destroyed, and those which must execute after.
  75.  *    We assume there is a non-standard _atexit() routine which can be used to install
  76.  *    the post-destruction exit handlers.
  77.  *
  78.  */
  79.  
  80. int __start(void)
  81. {
  82.     extern    int        __NubAt3;
  83.     extern    void    _BreakPoint(void);
  84.     extern    int        __C_phase;
  85.     extern    int        _exit_status;
  86.     extern    jmp_buf    __target_for_exit;
  87.     extern    struct {
  88.                 short    unknown;
  89.                 long    ArgC;
  90.                 char    **ArgV;
  91.                 char    **EnvP;
  92.             } _IntEnv;
  93.         
  94.     int        ret = 0;
  95.     char *argv = 0;
  96.     
  97.     if (__NubAt3 != 0) {
  98.         _BreakPoint();
  99.     }
  100.     
  101.     __C_phase = 2;
  102.     
  103.     if (setjmp(__target_for_exit) == 0) {    //    set up jmp_buf for exit()
  104.         __sinit();                        //    call all static initializers
  105.         _exit_status = main(_IntEnv.ArgC, _IntEnv.ArgV, _IntEnv.EnvP);
  106.         __destroy_global_chain();
  107.         exit(_exit_status);                // this will jump to setjmp following the atexit_hook()
  108.     }
  109.     return(_exit_status);
  110. }
  111.